﻿// MIT License
// 开源地址：https://gitee.com/co1024/AbcMvc
// Copyright (c) 2021-2023 1024
// Abc.Mvc=Furion+EF+SqlSugar+Pear layui admin.

using Lazy.SlideCaptcha.Core;
using Lazy.SlideCaptcha.Core.Validator;
using static Lazy.SlideCaptcha.Core.ValidateResult;

namespace Abc.Web.Mvc.Areas.HtAdmin.Controllers;

/// <summary>
/// 滑动验证码
/// </summary>
[AllowAnonymous]
public class CaptchaController : HtAdminBaseController
{
    private readonly Lazy.SlideCaptcha.Core.ICaptcha _captcha;

    public CaptchaController(Lazy.SlideCaptcha.Core.ICaptcha captcha)
    {
        _captcha = captcha;
    }

    /// <summary>
    /// id
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpGet]
    [AllowAnonymous]
    [NonUnify]
    public CaptchaData Generate()
    {
        return _captcha.Generate();
    }

    /// <summary>
    /// id
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpPost]
    [AllowAnonymous]
    [NonUnify]
    public ValidateResult Validate(string id, string trackJson)
    {

        var result = _captcha.Validate(id, trackJson.ToObj<SlideTrack>());


        //判断是否成功
        if (result.Result == ValidateResultType.Success)
        {
            //成功了添加缓存
            Caches.Set($"{id}_captchaId", true);

        }


        return result;
    }

    /// <summary>
    /// 滑动验证码页面
    /// </summary>
    /// <returns></returns>
    [HttpGet]
    [AllowAnonymous]
    public IActionResult Index()
    {
        return View();
    }


}
